
import '../../sass/auth/Role.scss';
import router from '../../router/Index.js';


export default {
    data(){
        return {
            search_key:'',
            tableData: [],
            loading:false,
            currentPage: 1,
            pageSize:15,
            totalSize: 10
        }
    },
    components:{

    },
    computed: {

    },
    created:function () {
        this.fetch_data(this.currentPage);
    },
    filters:{
        getDate(tm){
            let tt=new Date(parseInt(tm) * 1000).toLocaleString();
            return tt;
        }
    },
    methods: {
        handleIconClick(ev) {
            this.fetch_data();
        },
        handleEdit(index, row) {
            router.push({path : 'role/edit', query :{id : row.id}});
        },
        confirmDelete(index, row){
            this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.handleDelete(index, row);
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        async handleDelete(index, row) {
            let row_id = row.id;
            let _this=this,response,_response;
            let url = '/backend/role/del';
            this.loading = true;
            try {
                response = await fetch(url, {
                    method: 'POST',
                    headers: {
                        'Accept': 'application/json',
                        'Content-type': "application/json",
                    },
                    credentials: 'same-origin',
                    body:JSON.stringify({
                        'id' : row_id
                    })
                });
                _response = await response.json();

                if(_response.code===0){
                    _this.$message({
                        message: _response.msg,
                        type: 'success'
                    });
                    _this.fetch_data(_this.currentPage);
                }else if(_response.code ===-2){
                    _this.$message({
                        message: _response.msg,
                        type: 'error'
                    });
                }else if(_response.code ===-999){
                    _this.$message({
                        message: _response.msg,
                        type: 'error'
                    });
                    setTimeout(function () {
                        window.location.reload();
                    },1500);
                }
                _this.loading = false;
            } catch(err) {
                _this.$message({
                    message: '调用接口失败',
                    type: 'error'
                });
                _this.loading = false;
            }
        },
        toAddRole(){
            router.push({path : 'role/edit'});
        },
        async fetch_data( page = 0, ps = this.pageSize, sort = ''){
            let title = this.search_key;
            let _this=this,response,_response;
            let url = '/backend/role/roleList';
            this.loading = true;
            try {
                response = await fetch(url, {
                    method: 'POST',
                    headers: {
                        'Accept': 'application/json',
                        'Content-type': "application/json",
                    },
                    credentials: 'same-origin',
                    body:JSON.stringify({
                        'page':page,
                        'ps':ps,
                        'sort':sort,
                        'title':title
                    })
                });
                _response = await response.json();

                if(_response.code===0){

                    _this.tableData = _response.data.roleList;
                    _this.currentPage = _response.data.page;
                    _this.pageSize = _response.data.pageSize;
                    _this.totalSize = _response.data.amount;
                }else if(_response.code ===-1){
                    _this.$message({
                        message: _response.msg,
                        type: 'warning'
                    });
                }else if(_response.code ===-2){
                    _this.$message({
                        message: _response.msg,
                        type: 'error'
                    });
                }else if(_response.code ===-999){
                    _this.$message({
                        message: _response.msg,
                        type: 'error'
                    });
                    setTimeout(function () {
                        window.location.reload();
                    },1500);
                }
                _this.loading = false;
            } catch(err) {
                _this.$message({
                    message: '调用接口失败',
                    type: 'error'
                });
                _this.loading = false;
            }
        },
        handleSizeChange(val) {
            this.fetch_data(1,`${val}`);
        },
        handleCurrentChange(val) {
            this.fetch_data(`${val}`);
        },
        ManagerList(index, row) {
            router.push({path : 'role/manager', query :{id : row.id , name: row.title}});
        },
        PermissionList(index, row) {
            router.push({path : 'role/permission', query :{id : row.id , name: row.title}});
        },
        MenuList(index, row) {
            router.push({path : 'role/menu', query :{id : row.id , name: row.title}});
        }
    }
}